# -*- coding:utf-8 -*-
"""
作者：Wangzhicheng
日期：2024年08月23日14:38
@file 剪刀石头布游戏.py
"""
import random


# 可以创建游戏对象
# 游戏对象可以加入到房间
# 同一个房间内的两个游戏对象可以进行游戏
# 每次两个游戏对象随机的出剪刀石头布
# 三局两胜
# 玩家
# 属性 姓名、房间
# 方法    战斗
# 房间  房间号 、房间里的成员

# 无限输入
# 1、创建房间
# 2、创建角色
# 3、将角色加入房间
# 4、选择房间战斗
# 5、退出

class Player:
    def __init__(self, name, room=None):
        self.name = name
        self.room = room
        self.score = 0

    def fight(self, other):
        methods = {1: "剪刀", 2: "石头", 3: "布"}
        my_choice = input("剪刀(1)---石头(2)---布(3)---随机(其他输入)\n请输入您的选择:     (示例:1)\n")
        if my_choice != "1" and my_choice != "2" and my_choice != "3":
            print("输入有误，随机产生")
            my_choice = random.randint(1, 3)
            print("随机产生",my_choice)
        else:
            my_choice = int(my_choice)
        other_choice = random.randint(1, 3)

        # 根据出拳结果判断胜负
        if  (my_choice == 1 and other_choice == 3) or \
            (my_choice == 2 and other_choice == 1) or \
            (my_choice == 3 and other_choice == 2):
            self.score += 1
            result = f"{self.name} 胜！"
        elif my_choice == other_choice:
            result = "平局！"
        else:
            other.score += 1
            result = f"{other.name} 胜！"

        # 输出双方的选择和对局结果
        result = f"\n{self.name}: {methods[my_choice]} --------- {other.name}: {methods[other_choice]}\n" + result.center(30, "*")
        return result


class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.room_members = []

    def add_player(self, player):
        self.room_members.append(player)

# 随机对手名字
def random_chinese_char():
    unicode_start = 0x4e00
    unicode_end = 0x9fa5
    unicode_random1 = random.randint(unicode_start, unicode_end)
    unicode_random2 = random.randint(unicode_start, unicode_end)
    chinese_char1 = chr(unicode_random1)
    chinese_char2 = chr(unicode_random2)
    return chinese_char1+chinese_char2



print("正在创建房间......")
room_id = random.randint(1, 999)
room = Room(room_id)
print(f"房间号为{room_id}")

user1_name = input("请输入您的名字：")
player1 = Player(user1_name, room)

user2_name = random_chinese_char()
print(f"随机匹配对手：{user2_name}")
player2 = Player(user2_name, room)
room.add_player(player2)
# 三局两胜
rounds = 0
while player1.score < 2 and player2.score < 2:
    rounds += 1
    print(f"\n第{rounds}局:")
    print(player1.fight(player2))

if player1.score > player2.score:
    print(f"\n最终获胜者是：{player1.name}")
else:
    print(f"\n最终获胜者是：{player2.name}")
